package com.mesd.killer.dao.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import com.mesd.framework.dao.impl.BaseDaoImpl;
import com.mesd.framework.util.DateUtil;
import com.mesd.killer.dao.KillerDao;
import com.mesd.killer.filter.SearchBean;
import com.mesd.killer.model.GameResult;
import com.mesd.killer.constrant.ICompanyName;
import com.mesd.killer.constrant.IPankouType;
import com.mesd.killer.constrant.ISQLConstant;

@Component("killerDao")
public class KillerDaoImpl extends BaseDaoImpl implements KillerDao,ICompanyName,IPankouType,ISQLConstant {

	@Override
	public List<GameResult> getTodayGameResults() {
		Session session = getSession();
		Criteria criteria = session.createCriteria(GameResult.class);

		Calendar begin= Calendar.getInstance(); 	   
		begin.add(java.util.Calendar.HOUR_OF_DAY,-2); 
		
		Calendar end = Calendar.getInstance(); 	   
		end.add(java.util.Calendar.HOUR_OF_DAY,12); 
		
		Date low = begin.getTime();
		Date high = end.getTime();
		criteria.add(Restrictions.between(GameResult.GAME_TIME, new Timestamp(low.getTime()), new Timestamp(high.getTime())));
//		criteria.add(Restrictions.eq(GameResult.STATE, -1));
		criteria.addOrder(Order.asc(GameResult.GAME_TIME));
		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);		
		return criteria.list();
	}

	@Override
	public List<Long> searchGameIds(SearchBean searchBean) {
		long gameId = searchBean.getGameId();
		float shuiweiOffset = searchBean.getShuiweiOffset();
		int hasDaxiao = searchBean.isHasDaxiao();
		float daxiaoOffset = searchBean.getBigOffset();
		SQLQuery query = (SQLQuery) getSession().createSQLQuery("{call searchGameDetail(?,?,?,?)}");
		query.setLong(0,gameId);
		query.setInteger(1, hasDaxiao);
		query.setFloat(2, shuiweiOffset);
		query.setFloat(3, daxiaoOffset);
		List list = query.list();
		List<Long> result = new ArrayList<Long>();
		for (int i = 0; i < list.size(); i++) {
			result.add(Long.parseLong(list.get(i).toString()));
		}
		return result;
	}
	
	@Override
	public List<GameResult> getGameResults(List<Long> gameIds) {
		if(gameIds == null || gameIds.size()<1)
			return null;
		Criteria criteria = getSession().createCriteria(GameResult.class);
		criteria.add(Restrictions.in(GameResult.GAME_ID, gameIds));
		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		return criteria.list();
	}

	@Override
	public List<GameResult> getGameResults(Date start, Date end) {
		Criteria criteria = getSession().createCriteria(GameResult.class);
		criteria.add(Restrictions.between(GameResult.GAME_TIME, new Timestamp(start.getTime()), new Timestamp(end.getTime())));
		criteria.setFetchMode("gameDetails", FetchMode.JOIN);
		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		return criteria.list();
	}
	
	public List<Long> searchGameIdsByAll(SearchBean searchBean) {
		long gameId = searchBean.getGameId();
		float shuiweiOffset = searchBean.getShuiweiOffset();
		float oddsOffset = searchBean.getOddsOffset();
		float daxiaoOffset = searchBean.getBigOffset();
		SQLQuery query = (SQLQuery) getSession().createSQLQuery("{call SearchGameDetailByAll(?,?,?,?)}");
		query.setLong(0,gameId);
		query.setFloat(1, shuiweiOffset);
		query.setFloat(2, daxiaoOffset);
		query.setFloat(3, oddsOffset);
		List list = query.list();
		List<Long> result = new ArrayList<Long>();
		for (int i = 0; i < list.size(); i++) {
			result.add(Long.parseLong(list.get(i).toString()));
		}
		return result;
	}

	@Override
	public List<GameResult> getCurrentGameResult()
	{
		Criteria criteria = getSession().createCriteria(GameResult.class);
		Calendar begin= Calendar.getInstance(); 	   
		begin.add(java.util.Calendar.MINUTE,-4); 
		
		Calendar end = Calendar.getInstance(); 	   
		end.add(java.util.Calendar.MINUTE,1); 
		
		Date low = begin.getTime();
		Date high = end.getTime();
		criteria.add(Restrictions.between(GameResult.GAME_TIME, new Timestamp(low.getTime()), new Timestamp(high.getTime())));
//		criteria.add(Restrictions.eq(GameResult.STATE, -1));
		criteria.addOrder(Order.asc(GameResult.GAME_TIME));
		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);		
		return criteria.list();
	}

	@Override
	public List<GameResult> getDaqiuGameResult()
	{
		List<Long> daqiuGameIds = searchDaqiuGameIds();		
		return getGameResults(daqiuGameIds);
	}
	
	public List<Long> searchDaqiuGameIds() {
		SQLQuery query = (SQLQuery) getSession().createSQLQuery("{call searchDaqiuGameResult()}");
		List list = query.list();
		List<Long> result = new ArrayList<Long>();
		for (int i = 0; i < list.size(); i++) {
			result.add(Long.parseLong(list.get(i).toString()));
		}
		return result;
	}
}
